package cn.com.nes.site.service.product.impl;

import cn.com.nes.mybatis.agent.ope.entity.Sblx;
import cn.com.nes.mybatis.agent.ope.mapper.SblxMapper;
import cn.com.nes.mybatis.agent.system.mapper.TUserMapper;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.service.product.SblxMaintenanceService;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class SblxMaintenanceServiceImpl implements SblxMaintenanceService {
	private static int sblx;
	@Resource
	private SblxMapper sblxMapper;
	@Resource
	private TUserMapper tUserMapper;
	
	/**
	 * 添加设备
	 *
	 * @param sblxVo
	 * @return
	 */
	@Override
	public PageData addSblx(Sblx sblxVo) {
		if (Objects.isNull(sblxVo.getId())) {
			//添加
			sblxVo.setSfsc(0);
			sblxVo.setSblxId(sblxId(sblxVo));
			Integer insert = sblxMapper.save(sblxVo);
			System.out.println("sblxVo = " + sblxVo);
			if (insert > 0) {
				return PageData.ok();
			}
			return PageData.error("失败");
		}else {
			//更新
			String company = null;
			//判断登录用户身份
			if (!adminAuth(sblxVo.getRoleId())) {
				company = sblxVo.getCompanyId();
			}
			int update = sblxMapper.updatesblx(sblxVo,company);
			if (update > 0) {
				return PageData.ok();
			}
			return PageData.error("失败");
		}
	}
	
	/**
	 * 查看设备
	 *
	 * @param sblxVo
	 * @return
	 */
	@Override
	public PageData selectSblx(Sblx sblxVo) {
		final Page<Map<String, Object>> page = new Page<>(sblxVo.getPageid(), sblxVo.getPagesize());
		//管理员可以查看所有设备
		if (adminAuth(sblxVo.getRoleId())) {
			if (sblxVo.getRoleId() == "00"){
			sblxVo.setCompanyId("");}
		}
		List<Map<String, Object>> sblxList = sblxMapper.selectPageList(page, sblxVo);
		for (Map<String, Object> sblx : sblxList) {
//			String companyId = sblx.getCompanyId();
			//企业id
			//设计来源id
			String sjly = (String) sblx.get("sjly");
			sblx.put("sjlyname", tUserMapper.selectSjlyName(sjly));
			String cpmc = (String) sblx.get("cpmc");
			if (StringUtils.isEmpty(cpmc)){
				sblx.put("cpmc", "");
			}
		}
		page.setRecords(sblxList);
		return PageData.ok(page);
	}
	

	
	/**
	 * 删除设备
	 *
	 * @param sblxVo
	 * @return
	 */
	@Override
	public PageData deleteSblx(Sblx sblxVo) {
		sblxVo.setSfsc(1);
		QueryWrapper<Sblx> wrapper = new QueryWrapper();
//		判断登录身份
		if (adminAuth(sblxVo.getRoleId())) {
			wrapper.eq("sfsc", 0);
			wrapper.eq("id", sblxVo.getId());
		} else {
			wrapper.eq("sfsc", 0);
			wrapper.eq("id", sblxVo.getId());
			wrapper.eq("companyId", sblxVo.getCompanyId());
		}
		int delete = sblxMapper.update(sblxVo, wrapper);
		if (delete > 0) {
			return PageData.ok();
		}
		return PageData.error("删除失败");
	}
	
	/**
	 * 生成sblxId
	 *
	 * @return
	 */
	private String sblxId(Sblx sblxVo) {
		//判断添设备人员身份设置sblxid格式
		String sbid;
		if (adminAuth(sblxVo.getRoleId())) {
			sbid = "sblx_" + UUID.randomUUID().toString().replaceAll("-", "");
		} else {
			sbid = sblxVo.getSbcj() + String.format("%03d", ++sblx);
		}
		return sbid;
	}
	
	
	/**
	 * 是否有管理员权限
	 *
	 * @return
	 */
	private boolean adminAuth(String roleId) {
		Map roule = tUserMapper.adminAuth(roleId);
		if (Objects.isNull(roule)) {
			return false;
		}
		return true;
	}
}
